let cnn = require('./dbManager');
let sqlstring = require('sqlstring');
let uuid = require('uuid/v4');

let mm = {
    addBrand: function (info, cb) {
        info.brandid = uuid();
        let sf = "insert into t_brand set ?"
        let sql = sqlstring.format(sf, info);
        console.log(sql);
        cnn.query(sql, function (err, results) {
            if (err) {
                cb(400);
                console.error(err);
                return;
            }
            cb(200);
            console.log(JSON.stringify(results));
        });
    },
    removeBrand: function (info, cb) {
        let sf = "delete from t_brand where ??=?"
        let sql = sqlstring.format(sf, info);
        console.log(sql);
        cnn.query(sql, function (err, results) {
            if (err) {
                cb(400);
                console.error(err);
                return;

            }
            console.log(JSON.stringify(results));
        });
    },
    updateBrand: function (info, cb) {
        let params = [info, "brandid", info['brandid']];
        let sf = "update t_brand set ? where ??=?"
        let sql = sqlstring.format(sf, params);
        console.log(sql);
        cnn.query(sql, function (err, results) {
            if (err) {
                cb(400);
                console.error(err);
                return;
            }
            cb(200);
            console.log(JSON.stringify(results));
        });
    },
    getBrandList: function (info, cb) {
        let pageSize = info['pageSize'];
        if(pageSize == undefined) pageSize = 20;
        let sql = `select * from t_brand limit ${info.page},${pageSize}`;
        cnn.query(sql, function (err, results) {
            if (err) {
                cb(400);
                console.error(err);
                return;
            }
            cb(200);
            console.log(JSON.stringify(results));
        });
    }
};


// mm.addBrand({
//     name: "腾讯222",
//     tradeid: "53dccff0-0f96-4685-85b0-5ecfb231bf32",
//     category: "西红柿-吵到",
//     product: "方便面",
//     logourl: "http://123.baidu.com",
//     uid: "123"
// });

// mm.updateBrand({
//     name: "腾讯333333",
//     tradeid: "53dccff0-0f96-4685-85b0-5ecfb231bf32",
//     category: "西红柿-吵到",
//     // product: "方便面",
//     logourl: "http://123.baidu.com",
//     uid: "123",
//     brandid: "40b46a68-7542-478d-b416-7bf91f84a3f6"
// });

// mm.removeBrand(["brandid", "ae89202f-6372-4931-aa59-be6b6030c6b2"], null);